package com.sgave.blog.controller;

import com.sgave.blog.model.Result;
import com.sgave.blog.service.BlogService;
import com.sgave.blog.vo.BlogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fly
 * @date 2024/1/9 19:03
 */
@Api(tags = "博客管理")
@RestController
@RequestMapping("/blog")
@CrossOrigin
public class BlogController {

    @Resource
    private BlogService blogService;

    @ApiOperation(value = "新增博客")
    @PostMapping("add")
    public Result addBlog(@RequestBody BlogVo blogVo){
        int result = blogService.addBlog(blogVo);
        if (result > 0) {
            return Result.success("新增成功", blogVo);
        } else {
            return Result.error("新增失败");
        }
    }

    @ApiOperation(value = "查询所有博客")
    @GetMapping("selectAll")
    public Result selectAll(){
        List<BlogVo> blogList = blogService.selectAll();
        return Result.success(blogList);
    }

    @ApiOperation(value = "根据博客id查询博客")
    @GetMapping("/{id}")
    public Result selectAll(@PathVariable Integer id){
        BlogVo blogVo = blogService.selectById(id);
        return Result.success(blogVo);
    }

    @ApiOperation(value = "根据作者或标题查询博客")
    @GetMapping("selectByParam")
    public Result selectByParam(String keyword, Integer labelId){
        List<BlogVo> blogList = blogService.selectByParam(keyword, labelId);
        return Result.success(blogList);
    }

    @ApiOperation(value = "查询热点信息")
    @GetMapping("selectHotNews")
    public Result selectHotNews(){
        List<BlogVo> blogList = blogService.selectHotNews(5);
        return Result.success(blogList);
    }
}
